﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;

namespace hbse
{
    public partial class BaseForm : Form
    {
        private readonly bool isStandAlone = true;
        private ISession session;

        public BaseForm()
        {
            InitializeComponent();
        }

        public BaseForm(ISession session)
            : this()
        {
            if (session != null)
            {
                isStandAlone = false;
            }
            this.session = session;
        }

        protected ISession Session
        {
            get
            {
                if (session == null || !session.IsOpen)
                {
                    if (session != null)
                    {
                        session.Dispose();
                    }
                    session = ApplicationContext.SessionFactory.OpenSession();
                    session.FlushMode = FlushMode.Never;
                }
                return session;
            }
        }

        public bool IsStandAlone
        {
            get { return isStandAlone; }
        }

        private void BaseForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (isStandAlone && session != null)
            {
                session.Dispose();
            }
        }

        private void BaseForm_Load(object sender, EventArgs e)
        {

        }

        
    }
}
